package net.liuzd.spring.boot.v2.web;

import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import lombok.extern.log4j.Log4j2;
import net.liuzd.spring.boot.v2.enums.OptionEnum;
import net.liuzd.spring.boot.v2.service.EChartsService;
import net.liuzd.spring.boot.v2.service.PhantomjsService;
import net.liuzd.spring.boot.v2.util.Base64ImgsUtil;

@Controller
@RequestMapping("echarts")
@Log4j2
public class EChartsController {

    @Autowired
    private EChartsService   echartsService;
    
    @Autowired
    private PhantomjsService phantomjsService;

    @Value("${image.root.path}")
    private String           imageOutPath;

    @GetMapping
    public String index(Map<String, Object> model) {
        model.put("options", OptionEnum.values());
        return "echarts/index";
    }

    @RequestMapping(value = "/line", method = RequestMethod.GET, produces = "text/html; charset=UTF-8")
    @ResponseBody
    public String line() {
        return echartsService.line();
    }

    @RequestMapping(value = "/{type}.html", method = RequestMethod.GET)
    public String option(@PathVariable("type") String type, Map<String, Object> model) {
        model.put("option", echartsService.option(type));
        return "echarts/view";
    }

    @RequestMapping(value = "/phantomjs", method = RequestMethod.GET)
    public String phantomjs(Map<String, Object> model) {
        return "phantomjs";
    }

    @RequestMapping(value = "/createPhantomjs", method = RequestMethod.GET)
    public void createPhantomjs(Map<String, Object> model) throws IOException {
        phantomjsService.execPhantomJs("http://localhost/echarts/phantomjs");
    }

    @RequestMapping(value = "/demo", method = RequestMethod.GET)
    public String demo(Map<String, Object> model) {
        return "echarts/demo";
    }

    /**
     * Echart图片生成
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "saveImage", method = RequestMethod.POST)
    public void saveEchartImage(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String picInfo = request.getParameter("picInfo");
        UUID uuid = UUID.randomUUID();
        String name = uuid.toString();
        // 获取服务器根路径
        // getClass().getClassLoader().getResource("").getPath()
        File imgFolder = new File(imageOutPath);
        if (!imgFolder.exists()) {
            imgFolder.mkdirs();
        }
        File imgFile = new File(imgFolder, name + ".png");
        if (!imgFile.exists()) {
            imgFile.createNewFile();
        }
        String imgPath = imgFile.getAbsolutePath();// 图片存储路径
        log.info("saveEchartImage img is:" + imgPath);
        // 传递过程中 "+" 变为了 " ".
        String newPicInfo = picInfo.replaceAll(" ", "+");
        String[] arr = newPicInfo.split("base64,");
        Base64ImgsUtil.base64StringToImage(arr[1], imgPath);
        response.getWriter().write(name);
    }

}